package com.zj.common.mq;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jms.connection.CachingConnectionFactory;
import org.springframework.jms.connection.JmsTransactionManager;
import org.springframework.jms.connection.UserCredentialsConnectionFactoryAdapter;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.transaction.PlatformTransactionManager;

import com.ibm.mq.jms.MQQueueConnectionFactory;
import com.ibm.msg.client.wmq.WMQConstants;

@Configuration
public class JmsConfig {
	@Value("${mq.host}")
	private String host;
	@Value("${mq.port}")
	private Integer port;
	@Value("${mq.queue-manager}")
	private String queueManager;
	@Value("${mq.username}")
	private String username;
	@Value("${mq.password}")
	private String password;
	@Value("${mq.receive-timeout}")
	private long receiveTimeout;

	@Value("${mq.channel}")
	private String channel;

	 @Bean
	    public MQQueueConnectionFactory mqQueueConnectionFactory() {
	        MQQueueConnectionFactory mqQueueConnectionFactory = new MQQueueConnectionFactory();
	        mqQueueConnectionFactory.setHostName(host);
	        try {
	            mqQueueConnectionFactory.setTransportType(WMQConstants.WMQ_CM_CLIENT);
	            mqQueueConnectionFactory.setCCSID(819);
	            mqQueueConnectionFactory.setChannel(channel);
	            mqQueueConnectionFactory.setPort(port);
	            mqQueueConnectionFactory.setQueueManager(queueManager);
	         } catch (Exception e) {
	            e.printStackTrace();
	        }
	        return mqQueueConnectionFactory;
	    }
	    @Bean
	    UserCredentialsConnectionFactoryAdapter userCredentialsConnectionFactoryAdapter(MQQueueConnectionFactory mqQueueConnectionFactory) {
	        UserCredentialsConnectionFactoryAdapter userCredentialsConnectionFactoryAdapter = new UserCredentialsConnectionFactoryAdapter();
	        userCredentialsConnectionFactoryAdapter.setUsername(username);
	        userCredentialsConnectionFactoryAdapter.setPassword(password);
	        userCredentialsConnectionFactoryAdapter.setTargetConnectionFactory(mqQueueConnectionFactory);
	        return userCredentialsConnectionFactoryAdapter;
	    }
	    @Bean
	    @Primary
	    public CachingConnectionFactory cachingConnectionFactory(UserCredentialsConnectionFactoryAdapter userCredentialsConnectionFactoryAdapter) {
	        CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();
	        cachingConnectionFactory.setTargetConnectionFactory(userCredentialsConnectionFactoryAdapter);
	        cachingConnectionFactory.setSessionCacheSize(500);
	        cachingConnectionFactory.setReconnectOnException(true);
	        return cachingConnectionFactory;
	    }
	    @Bean
	    public PlatformTransactionManager jmsTransactionManager(CachingConnectionFactory cachingConnectionFactory) {
	        JmsTransactionManager jmsTransactionManager = new JmsTransactionManager();
	        jmsTransactionManager.setConnectionFactory(cachingConnectionFactory);
	        return jmsTransactionManager;
	    }
	    @Bean
	    public JmsTemplate jmsOperations(CachingConnectionFactory cachingConnectionFactory) {
	        JmsTemplate jmsTemplate = new JmsTemplate(cachingConnectionFactory);
	        jmsTemplate.setReceiveTimeout(receiveTimeout);
	        return jmsTemplate;
	    }

	

}
